package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotMapPosDto;
import com.hitqz.robot.biz.mapper.RobotMapPosMapper;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.netty.service.Send2ServerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xupkun
 * @date 2024/5/29
 */
@Service
@AllArgsConstructor
public class RobotMapPosServiceImpl extends ServiceImpl<RobotMapPosMapper, RobotMapPos> implements RobotMapPosService {

    private final Send2ServerService send2ServerService;

    private final RobotService robotService;

    @Override
    public void syncMapPos() {
        List<RobotMapPos> robotMapPosList = baseMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(robotMapPosList)) {
            Robot robot = robotService.getDefaultRobot();
            robotMapPosList.forEach(pos-> {
                RobotMapPosDto robotMapPosDto = new RobotMapPosDto();
                BeanUtil.copyProperties(pos, robotMapPosDto, false);
                robotMapPosDto.setClientId(robot.getRobotSn());
                send2ServerService.syncRobotMapPosInfo(0, robotMapPosDto);
            });
        }
    }
}
